package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 等级管理控制器
 */
@RestController
@RequestMapping("/admin/level")
@Tag(name = "等级管理", description = "等级管理相关接口")
@Slf4j
public class LevelController {

    @Autowired
    private LevelService levelService;

    /**
     * 添加等级
     *
     * @param command 等级命令
     * @return 结果
     */
    @PostMapping
    @Operation(summary = "添加等级", description = "主要用于等级特权管理-等级管理添加用户等级")
    public Result<Void> addLevel(@RequestBody LevelCommand command) {
        levelService.addLevel(command);
        return Result.ok();
    }

    /**
     * 修改等级
     *
     * @param command 等级命令
     * @return 结果
     */
    @PutMapping
    @Operation(summary = "修改等级", description = "主要用于等级特权管理-等级管理更新用户等级信息")
    public Result<Void> updateLevel(@RequestBody LevelCommand command) {
        levelService.updateLevel(command);
        return Result.ok();
    }

    /**
     * 获取等级列表
     *
     * @return 等级列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取等级列表", description = "主要用于等级特权管理-等级管理获取等级列表")
    public Result<PageDTO<LevelDTO>> getLevelList(PageRequest pageRequest) {
        log.info("接收到查询请求，页码: {}, 数量: {}, 关键字: {}", pageRequest.getPageNum(), pageRequest.getCount(), pageRequest.getKeyword());

        PageDTO<LevelDTO> pageDTO = levelService.getLevelList(pageRequest);
        return Result.ok(pageDTO);
    }

    /**
     * 删除指定的等级
     *
     * @param id 等级ID
     * @return 结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除等级", description = "主要用于等级特权管理-等级管理删除指定用户等级信息")
    public Result<DeleteLevelOperateDTO> deleteLevel(@PathVariable Long id) {
        log.info("删除等级, id: {}", id);
        DeleteLevelOperateDTO operateDTO = levelService.deleteLevel(id);
        return Result.ok(operateDTO);
    }

    /**
     * 批量删除等级
     *
     * @param command 批量删除命令
     * @return 结果
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除等级", description = "主要用于等级特权管理-等级管理批量删除用户的等级信息")
    public Result<List<DeleteLevelOperateDTO>> batchDeleteLevel(@RequestBody BatchDeleteCommand command) {
        log.info("批量删除等级, ids: {}", command.getIds());
        List<DeleteLevelOperateDTO> operateDTOList = levelService.deleteLevels(command);
        return Result.ok(operateDTOList);
    }
}
